package com.ltxtiyu.video.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import com.ltxtiyu.video.bean.VideoInfoBean;
import com.ltxtiyu.video.cacheDao.MomentCacheDao;
import com.ltxtiyu.video.cacheDao.VideoCacheDao;
import com.ltxtiyu.video.common.ConfigConstant;
import com.ltxtiyu.video.dao.ltx.*;
import com.ltxtiyu.video.dao.sjs.SjsVideoDao;
import com.ltxtiyu.video.entity.*;
import com.ltxtiyu.video.service.ms.VideoService;
import com.ltxtiyu.video.tool.AliVideoTool;
import com.ltxtiyu.video.tool.StringUtil;
import com.ltxtiyu.video.vo.ms.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private MomentDao momentDao;
    @Autowired
    private MomentCacheDao momentCacheDao;
    @Autowired
    private VideoCacheDao videoCacheDao;
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private TagDao tagDao;
    @Autowired
    private VideoTagExDao videoTagExDao;
    @Autowired
    private ConfigDao configDao;
    @Autowired
    private SjsVideoDao sjsVideoDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Map<String, Object> selectParamList(Map<String, Object> map, String userId) {
        Boolean flag = null;
        String accessKeyId = configDao.selectByCkey(ConfigConstant.ACCESSKEYID).getCvalue();
        String accessKeySecret = configDao.selectByCkey(ConfigConstant.ACCESSKEYSECRET).getCvalue();
        String vodDomain = configDao.selectByCkey(ConfigConstant.ALIVOD_URL).getCvalue();
        String alivodPlayAction = configDao.selectByCkey(ConfigConstant.ALIVOD_PLAY_ACTION).getCvalue();
        AliVideoTool instance = AliVideoTool.getInstance(accessKeyId, accessKeySecret, vodDomain, alivodPlayAction);

        int count = momentDao.findByParamCount(map);
        List<VideoVo> data= momentDao.findByParamList(map);
        for (VideoVo record :data){
            Integer publisher = record.getPublisher();
            record.setUserId(userId);
            if (StringUtil.isNotBlank(userId)){
                String[] split = userId.split(",");
                boolean contains = Arrays.asList(split).contains(publisher.toString());
                if(contains){
                    flag= true;
                }else{
                    flag =false;
                }
                record.setFlag(flag);
                record.setFirstUserId(split[0]);
            }
            Integer status = record.getStatus();
            if (status == 1 &&StringUtil.isNotBlank(record.getAliVideoId())){
//                Map<String, String> videoPlayUrl = instance.getVideoPlayUrl(record.getAliVideoId());
//                if (videoPlayUrl!= null ){
//                    record.setVideoFaceImage(videoPlayUrl.get("CoverURL"));
//                    record.setUrl(videoPlayUrl.get("PlayURL"));
//                }
                VideoInfoBean videoInfoBean = JSON.parseObject(record.getVideoInfo(), VideoInfoBean.class);
                if (videoInfoBean!= null &&StringUtil.isNotBlank(videoInfoBean.getCoverUrl())){
                    record.setVideoFaceImage(videoInfoBean.getCoverUrl());
                    record.setUrl(videoInfoBean.getPlayUrl());
                    record.setAliyunStatus("Normal");
                }else {
                    GetVideoInfoResponse.Video video = instance.queryVideoInfo(record.getAliVideoId());
                    if (video!= null){
                        record.setAliyunStatus(video.getStatus());
                    }

                }
        }

        }
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","");
        result.put("count",count);
        result.put("data",data);

        return result;
    }

    @Override
    public void delByID(Integer id) {
        Moment moment = momentDao.selectByPrimaryKey(id);
        Integer videoId = moment.getVideoId();
        //删除动态
        momentCacheDao.deleteByPrimaryKey(id);
        //删除视频
        videoCacheDao.deleteByPrimaryKey(videoId);
        //删除标签绑定关系
        videoTagExDao.deleteByVideoId(videoId);

    }

    @Override
    public void addTagByVideoId(String videoTags, Integer videoId) {
        videoTagExDao.deleteByVideoId(videoId);
            if (videoTags.contains(",")){
                String[] tagArray = videoTags.split(",");
                for (String name: tagArray){
                    //标签 唯一校验
                   Tag record = tagDao.findByName(name);
                   if (record != null){
                       Map<String,Object> param = new HashMap<>();
                       param.put("videoId",videoId);
                       param.put("tagId",record.getId());
                       VideoTagEx exist=  videoTagExDao.selectByCondition(param);
                       if (exist != null){
                           continue;
                       }
                       //视频绑定标签
                       VideoTagEx videoTagEx = new VideoTagEx();
                       videoTagEx.setTagId(record.getId());
                       videoTagEx.setVideoId(videoId);
                       videoTagEx.setCreateTime(new Date());
                       videoTagExDao.insertSelective(videoTagEx);

                   }else{

                       //插入标签
                       Tag tag = new Tag();
                       tag.setName(name);
                       tag.setCreateTime(new Date());
                       tagDao.insertSelective(tag);
                       Map<String,Object> param = new HashMap<>();
                       param.put("videoId",videoId);
                       param.put("tagId",tag.getId());
                       VideoTagEx exist=  videoTagExDao.selectByCondition(param);
                       if (exist != null){
                           continue;
                       }
                       //视频绑定标签
                       VideoTagEx videoTagEx = new VideoTagEx();
                       videoTagEx.setTagId(tag.getId());
                       videoTagEx.setVideoId(videoId);
                       videoTagEx.setCreateTime(new Date());
                       videoTagExDao.insertSelective(videoTagEx);
                   }

                }
            }else{
                //标签 唯一校验
                Tag record = tagDao.findByName(videoTags);
                if (record != null){
                    Map<String,Object> param = new HashMap<>();
                    param.put("videoId",videoId);
                    param.put("tagId",record.getId());
                    VideoTagEx exist=  videoTagExDao.selectByCondition(param);
                    if (exist == null){

                        //视频绑定标签
                        VideoTagEx videoTagEx = new VideoTagEx();
                        videoTagEx.setTagId(record.getId());
                        videoTagEx.setVideoId(videoId);
                        videoTagEx.setCreateTime(new Date());
                        videoTagExDao.insertSelective(videoTagEx);
                    }

                }else{

                    //插入标签
                    Tag tag = new Tag();
                    tag.setName(videoTags);
                    tag.setCreateTime(new Date());
                    tagDao.insertSelective(tag);
                    Map<String,Object> param = new HashMap<>();
                    param.put("videoId",videoId);
                    param.put("tagId",tag.getId());
                    VideoTagEx exist=  videoTagExDao.selectByCondition(param);
                    if (exist == null){

                        //视频绑定标签
                        VideoTagEx videoTagEx = new VideoTagEx();
                        videoTagEx.setTagId(tag.getId());
                        videoTagEx.setVideoId(videoId);
                        videoTagEx.setCreateTime(new Date());
                        videoTagExDao.insertSelective(videoTagEx);
                    }
                }

            }


    }

    @Override
    public List<Tag> findTagByVideoId(Integer videoId) {
        return tagDao.findTagByVideoId(videoId);
    }

    @Override
    public void updateMoment(Moment moment) {
        momentCacheDao.updateByPrimaryKeySelective(moment);
    }

    @Override
    public Boolean addVideo(Moment moment, String aliVideoId) {
        Boolean flag = null;
        Video record =   videoDao.findByaliVideoId(aliVideoId);
        if (record == null && StringUtil.isNotBlank(aliVideoId)){
            //视频信息
            String accessKeyId = configDao.selectByCkey(ConfigConstant.ACCESSKEYID).getCvalue();
            String accessKeySecret = configDao.selectByCkey(ConfigConstant.ACCESSKEYSECRET).getCvalue();
            String vodDomain = configDao.selectByCkey(ConfigConstant.ALIVOD_URL).getCvalue();
            String alivodPlayAction = configDao.selectByCkey(ConfigConstant.ALIVOD_PLAY_ACTION).getCvalue();
            AliVideoTool instance = AliVideoTool.getInstance(accessKeyId, accessKeySecret, vodDomain, alivodPlayAction);
            Map<String, String> videoPlayUrl = instance.getVideoPlayUrl(aliVideoId);
            if (videoPlayUrl!= null){

                VideoInfoBean videoInfoBean = new VideoInfoBean();
                videoInfoBean.setCoverUrl(videoPlayUrl.get("CoverURL"));
                videoInfoBean.setDuration(videoPlayUrl.get("Duration"));
                videoInfoBean.setPlayUrl(videoPlayUrl.get("PlayURL"));
                //入库
                Integer publisher = moment.getPublisher();
                User recordUser = userDao.selectByPrimaryKey(publisher);
                if (recordUser != null){
                    Video video = new Video();
                    video.setVideoId(aliVideoId);
                    video.setStatus(1);
                    video.setVideoInfo(JSONObject.toJSONString(videoInfoBean));
                    video.setCreateTime(new Date());
                    video.setMadeTime(new Date());
                    videoCacheDao.insertSelective(video);

                    moment.setVideoId(video.getId());
                    moment.setCreateTime(new Date());
                    moment.setStatus(1);
                    moment.setPermissionType(110);
                    momentCacheDao.insertSelective(moment);
                    flag= true;
                }else{
                    flag= false;
                }
            }
        }
        return flag;
    }


}
